package cn.iocoder.yudao.module.project.controller.admin.internalworkstationplan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.internalworkstationplan.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.internalworkstationplan.InternalWorkstationPlanDO;
import cn.iocoder.yudao.module.project.service.internalworkstationplan.InternalWorkstationPlanService;

@Tag(name = "管理后台 - 内码工位计划")
@RestController
@RequestMapping("/project/internal-workstation-plan")
@Validated
public class InternalWorkstationPlanController {

    @Resource
    private InternalWorkstationPlanService internalWorkstationPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建内码工位计划")
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:create')")
    public CommonResult<String> createInternalWorkstationPlan(@Valid @RequestBody InternalWorkstationPlanSaveReqVO createReqVO) {
        return success(internalWorkstationPlanService.createInternalWorkstationPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内码工位计划")
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:update')")
    public CommonResult<Boolean> updateInternalWorkstationPlan(@Valid @RequestBody InternalWorkstationPlanSaveReqVO updateReqVO) {
        internalWorkstationPlanService.updateInternalWorkstationPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内码工位计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:delete')")
    public CommonResult<Boolean> deleteInternalWorkstationPlan(@RequestParam("id") String id) {
        internalWorkstationPlanService.deleteInternalWorkstationPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内码工位计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:query')")
    public CommonResult<InternalWorkstationPlanRespVO> getInternalWorkstationPlan(@RequestParam("id") String id) {
        InternalWorkstationPlanDO internalWorkstationPlan = internalWorkstationPlanService.getInternalWorkstationPlan(id);
        return success(BeanUtils.toBean(internalWorkstationPlan, InternalWorkstationPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内码工位计划分页")
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:query')")
    public CommonResult<PageResult<InternalWorkstationPlanRespVO>> getInternalWorkstationPlanPage(@Valid InternalWorkstationPlanPageReqVO pageReqVO) {
        PageResult<InternalWorkstationPlanDO> pageResult = internalWorkstationPlanService.getInternalWorkstationPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InternalWorkstationPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内码工位计划 Excel")
    @PreAuthorize("@ss.hasPermission('project:internal-workstation-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInternalWorkstationPlanExcel(@Valid InternalWorkstationPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InternalWorkstationPlanDO> list = internalWorkstationPlanService.getInternalWorkstationPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内码工位计划.xls", "数据", InternalWorkstationPlanRespVO.class,
                        BeanUtils.toBean(list, InternalWorkstationPlanRespVO.class));
    }


    @GetMapping("/checkAndCompletePlan")
    public CommonResult<Boolean> checkAndCompletePlan(@RequestParam(name = "orderCode") String orderCode) {
        internalWorkstationPlanService.checkAndCompletePlan(orderCode);
        return CommonResult.success(true);
    }

}